package com.hn.zhijian.utils;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author carl chen
 * @Classname DownloadService
 * @Description TODO
 * @Date 2019/11/5 10:24
 */
public class DownloadUtil {

//    public static void getPicture2(String urlHttp, String path){
//        FileOutputStream out = null;
//        BufferedInputStream in = null;
//        HttpURLConnection connection = null;
//        byte[] buf = new byte[1024];
//        int len = 0;
//        try {
//            URL url = new URL(urlHttp);
//            connection = (HttpURLConnection)url.openConnection();
//            connection.connect();
//            in = new BufferedInputStream(connection.getInputStream());
//            out = new FileOutputStream(path + "/" + new Date().getTime() + ".jpg");
//            while ((len = in.read(buf)) != -1) {
//                out.write(buf, 0, len);
//            }
//            out.flush();
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            try {
//                in.close();
//                out.close();
//                connection.disconnect();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//    }

    /**
     * 根据url返回文件的inputStream
     *
     * @param httpUrl
     * @return
     */
    public static InputStream getUrlInputStream(String httpUrl) throws IOException {
        return getHttpURLConnection(httpUrl).getInputStream();
    }

    /***
     * 获取连接
     * @param httpUrl
     * @return
     * @throws IOException
     */
    public static HttpURLConnection getHttpURLConnection(String httpUrl) throws IOException {
        return (HttpURLConnection) new URL(httpUrl).openConnection();
    }


    /**
     * inputstream转byte
     */
    public static byte[] toInputStream(InputStream inputStream) {

        ByteArrayOutputStream output = new ByteArrayOutputStream();

        byte[] buffer = new byte[1024 * 4];

        int len = 0;
        try {
            while ((len = inputStream.read(buffer)) != -1) {
                output.write(buffer, 0, len);
            }
        } catch (Exception e) {
            e.getMessage();
        } finally {
            try {
                output.close();
                inputStream.close();
            } catch (Exception e) {

            }
        }
        return output.toByteArray();
    }

    /**
     * 下载文件
     *
     * @param path
     * @param isShortTime 是否是临时文件默认false
     * @return 返回文件名称
     */
    public static String downloadFile(String path, String formatName, String outPath, boolean isShortTime) {

        FileOutputStream out = null;

        BufferedInputStream in = null;

        HttpURLConnection connection = null;

        String fileLocalPath = null;

        byte[] buf = new byte[1024];

        int len = 0;

        try {
            connection = getHttpURLConnection(path);

            connection.connect();

            in = new BufferedInputStream(connection.getInputStream());

            String tge = isShortTime ? "LS" : "YJ";

            String localPath =  outPath + "/" + UUID.getUUIdNo(tge);

            fileLocalPath = localPath + formatName;

            File file = new File(outPath);

            if(!file.isDirectory()){
                file.mkdirs();
            }

            out = new FileOutputStream(fileLocalPath);

            while ((len = in.read(buf)) != -1) {

                out.write(buf, 0, len);
            }

            out.flush();
        } catch (Exception e) {

        } finally {
            try {
                connection.disconnect();
                in.close();
                out.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return fileLocalPath;
    }

    /**
     * 下载图片
     *
     * @param path
     * @param outPath
     * @return
     */
    public static String downloadImg(String path, String outPath) {
        return downloadFile(path, ".jpg", outPath, true);
    }
}
